package edu.service.impl;

import edu.service.NoticeService;
import edu.vo.Notice;
import edu.repository.NoticeRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class NoticeServiceImpl implements NoticeService {

    private final NoticeRepository noticeRepository;

    public NoticeServiceImpl(NoticeRepository noticeRepository) {
        this.noticeRepository = noticeRepository;
    }

    @Override
    public Notice saveNotice(Notice notice) {
        return noticeRepository.save(notice);
    }

    @Override
    public Optional<Notice> getNoticeById(Integer id) {
        return noticeRepository.findById(id);
    }

    @Override
    public List<Notice> getAllNotices() {
        return noticeRepository.findAll();
    }

    @Override
    public void deleteNoticeById(Integer id) {
        noticeRepository.deleteById(id);
    }

    @Override
    public Notice updateNotice(Integer id, Notice notice) {
        return noticeRepository.findById(id)
                .map(existingNotice -> {
                    existingNotice.setTitle(notice.getTitle());
                    existingNotice.setAuthor(notice.getAuthor());
                    existingNotice.setContent(notice.getContent());
                    existingNotice.setType(notice.getType());
                    existingNotice.setReleasedate(notice.getReleasedate());
                    return noticeRepository.save(existingNotice);
                })
                .orElse(null);
    }
}